﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;
public partial class miniweb_products_detail : System.Web.UI.Page
{
    private long id = 0;
    private long Id = 0;//Id cua company
    ProductSell productSell = null;
    protected string nameCompany = string.Empty;
    protected string emailCompany = string.Empty;

    private void loadData()
    {
        productSell = ProductSellService.GetProductSell(id);
        ltrTitle.Text = productSell.Title;
        imgPro.Src = productSell.Image;
        ltrDescription.Text = productSell.Description;
        ltrEnd.Text = productSell.ProductDescription;
    }
    #region Load other product
    private void loadOtherProduct()
    {
        Criteria cri = new Criteria();
        cri.add(ProductSell._Table + "." + ProductSellColumns.CatId, productSell.CatId, Condition.EQUAL, Condition.AND);
        ProductSellCollection _ProductSellCollection = ProductSellService.GetProductSellListByParam(ProductSellColumns.UpdatedDate, OrderDirection.DESC, cri.Criter);
        if (_ProductSellCollection.Count > 0)
        {
            rptPro.DataSource = _ProductSellCollection;
            rptPro.DataBind();
        }        
    }

    protected void rptPro_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ProductSell prosell = (ProductSell)e.Item.DataItem;
        HtmlImage imgPro = (HtmlImage)e.Item.FindControl("imgPro");
        Literal ltrTitlePro = (Literal)e.Item.FindControl("ltrTitlePro");
        imgPro.Src = prosell.Image;
        ltrTitlePro.Text = prosell.Title;
    }
    #endregion
    





    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlAnchor aMenuProduct = (HtmlAnchor)Page.Master.FindControl("aMenuProduct");
        aMenuProduct.Attributes.Add("class", "Select");
        if (Request.QueryString["id"] != null)
        {
            id = long.Parse(Request.QueryString["id"].ToString());
            loadData();
            loadOtherProduct();
        }
        //System.Windows.Forms.MessageBox.Show(id.ToString());
    }



    #region Send Mail
    #region Send Contact
    #region Get Email Send To Admin
    private string EmailSend()
    {


        Criteria criuser = new Criteria();
        ADESSA.Entities.UserCollection _UserCollection = UserService.GetUserListByParam(UserColumns.UserId, OrderDirection.ASC, criuser.Criter);
        User _User = new User();
        if (_UserCollection.Count > 0)
        {
            _User = _UserCollection[0];
            return _User.Email.ToString();
        }
        else
            return RTISettings.EmailAdministrator;

    }
    #endregion


    #region Send Mail
    protected void SendMail(string email, string namecompany, string content)
    {
        Criteria _cri = new Criteria();
        _cri.add("[" + NewsletterColumns.Order + "]", FetConstant.EmailSendContactForCompany, Condition.EQUAL, Condition.AND);
        NewsletterCollection _NewsletterCollection = NewsletterService.GetNewsletterListByParam("[" + Newsletter._Table + "].[" + NewsletterColumns.CreatedDate + "]", OrderDirection.ASC, _cri.Criter);
        if (_NewsletterCollection != null)
        {
            if (_NewsletterCollection.Count > 0)
            {

                string subject = _NewsletterCollection[0].Title.Replace("Email_", "").ToString();

                string mailContent = string.Format(_NewsletterCollection[0].Content, namecompany, content);
                if (!Utility.SendMailDisplay(email, subject, mailContent, FetConstant.DisplayNameEmail, EmailSend()))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                if (!Utility.SendMailDisplay(EmailSend(), subject, mailContent, FetConstant.DisplayNameEmail, email))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                johndoe.Value = string.Empty;
                Alert.Show("Send email for company sussecc");
            }
        }



    }

    #endregion

    #endregion
    #endregion
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Session["CompanyID"] != null)
        {

            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
                nameCompany = _Company.CompanyName;
                emailCompany = _Company.Email;
                SendMail(emailCompany, nameCompany, johndoe.Value);
            }
        }
    }
}
